<template>
	<view class="terms-modal-container">
		<!-- 弹窗 头像昵称填写 -->
		<u-popup ref="popup_terms" round="16rpx" :show="showTerms" mode="center" :safeAreaInsetBottom="false"
			@close="handleCloseTerms" @open="handleOpenTerms">
			<view class="terms-modal-wrap">
				<view class="terms-modal-inner">
					<view class="modal-title">
						用户协议与隐私政策
					</view>
					<view class="modal-content">
						在使用服务之前，请仔细阅读 <text @click="toTerms()">《隐私条款》</text>
						<!-- 与 <text>《隐私保护指引》</text> -->
						的条款内容，以了
						解您的权利与义务。点击同意即表示您已
						充分阅读并接受以上协议内容。
					</view>
					<view class="modal-btns flex-center">
						<view class="btn btn-cancel flex-center" @click="doCancel()">
							暂不使用
						</view>
						<view class="btn btn-confirm flex-center">
							<button v-if="!has_phone" open-type="getPhoneNumber" class="btn-bind-phone flex-center"
								@getphonenumber="getPhoneNumber">
								同意并接受
							</button>
							<button v-else class="btn-bind-phone flex-center" @click="doAgree()">
								同意并接受
							</button>

						</view>
					</view>
				</view>
			</view>
		</u-popup>
		<!-- 弹窗 头像昵称填写 -->
	</view>
</template>

<script>
	export default {
		name: "terms-modal",
		data() {
			return {
				showTerms: true,
				weixin_code_login_info: {},
				has_avatar: false, //是否设置头像
				has_phone: true, //是否绑定手机号
				session_key: '',
				user: {
			
				},
				myInfo: {},
				token: '',
				userId: '',
			};
		},
		created() {
			this.get_weixin_code();
		},
		methods: {
			handleOpenTerms() {},
			handleCloseTerms() {},
			toTerms() {
				this.toRoute('/login-terms')
				// uni.navigateTo({
				// 	url: '/pages/login/terms'
				// })
			},
			init() {
				this.showTerms = true;
			},
			//授权获取code
			get_weixin_code() {
				uni.login({
					provider: 'weixin',
					success: (e) => {
						this.weixin_login(e.code);
					}
				});
			},
			//微信登录
			weixin_login(code) {
				this.showLoading();
				
				let invite_id = uni.getStorageSync('cache_invite_id') || ''
				

				this.$api({
					url: '/service.php',
					method: 'post',
					data: {
						action: 'login_miniLogin', //login
						code: code,
						invite_id: invite_id
					},
				}).then(res => {
					this.$logjson('login', res)
					if (res.code == 200) {
						let data = res.data;
						this.token = data.token;
						this.userId = data.userId;
						
						this.query_user(data);
						
						// this.has_avatar = !!data.image;
						// this.has_phone = !!data.mobile;
						// this.session_key = data.session_key || '';
					}
				})
			},

			//查询用户信息
			query_user() {
				this.$api({
					url: '/service.php',
					method: 'post',
					data: {
						action: 'users_userInfo', //login
						userId: this.userId || '',
						token: this.token || '',
						// code: code,
						// invite_id: this.invite_id
					},
				}).then(res => {
					if (res.code == 200) {
						let data = res.data;
						this.myInfo = data;
						this.has_avatar = !!data.image;
						this.has_phone = data.ifNeedBindPhone != 1;//是否需要注册绑定手机号
						
						this.$emit('changePhoneStatus', this.has_phone)
					}
					
					this.hideLoading()
				})
			},



			//获取手机号
			getPhoneNumber(e) {
				// console.warn('获取手机号', e)
				if (e.detail.errMsg.includes('fail')) {
					this.$alert('已取消')
					return
				}
				if (!e.detail.encryptedData) {
					return
				}

				this.$api({
					url: '/service.php',
					method: 'post',
					data: {
						action: 'login_bindWxPhone', //login
						encryptedData: e.detail.encryptedData,
						iv: e.detail.iv,
						// session_key: this.session_key || '',
						userId: this.userId,
						token: this.token,
					},
				}).then(res => {
					this.$logjson('bind', res)
					if (res.code == 200) {
						this.$store.commit('set_vuex_user', res.data)
						this.toRoute({
							path: '/login-next',
							mode: 'redirectTo'
						})
						// uni.navigateTo({
						// 	url: '/pages/login/login-next'
						// })
					}
				});
			},

			doCancel() {
				this.showTerms = false;
			},
			doAgree() {
				this.$store.commit('set_vuex_user', this.myInfo)
				this.$emit('confirm', {
					type: '同意协议',
					data: this.myInfo
				})
				this.showTerms = false;
			}

		}
	}
</script>

<style lang="scss" scoped>
	// 协议样式
	.terms-modal-wrap {
		width: 684rpx;
		width: 574rpx;

		min-height: 516rpx;
		background: #FFFFFF;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		padding: 60rpx 32rpx;
		overflow: hidden;

		display: flex;
		flex-direction: column;
		align-items: center;


		.terms-modal-inner {
			.modal-title {
				text-align: center;
				font-family: PingFang SC, PingFang SC;
				font-weight: 800;
				font-size: 32rpx;
				color: #000000;
			}

			.modal-content {
				margin: 50rpx 0;
				line-height: 1.6;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #828282;

				text {
					color: #FF0000;
				}
			}

			.modal-btns {
				.btn+.btn {
					margin-left: 18rpx;
				}

				.btn-cancel {
					flex: 1;
					// width: 246rpx;
					height: 80rpx;
					background: #F6FAFD;
					border-radius: 64rpx 64rpx 64rpx 64rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #828282;
				}

				.btn-confirm {
					flex: 1;
					// width: 246rpx;
					height: 80rpx;
					background: #CB0011;
					border-radius: 64rpx 64rpx 64rpx 64rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #FFFFFF;
					overflow: hidden;

					.btn-bind-phone {
						width: 100%;
						height: 100%;
						background: #CB0011;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 28rpx;
						color: #FFFFFF;
					}
				}
			}

		}
	}

	// 协议样式
</style>